﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QBluetoothSocket>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include <QLowEnergyCharacteristic>
#include <QLowEnergyDescriptor>
#include <QBluetoothUuid>
#include <QTimer>
#include <QLabel>

#include <QtCharts/QChart>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include "qcustomplot.h"
#include "myTasks.h"
#include <QThread>
#include "Bluetooth.h"
#include "Logger.h"
#include "myChartView.h"
QT_CHARTS_USE_NAMESPACE

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#define axisThread   0

namespace Ui {
class MainWindow;
}



class MainWindow : public QMainWindow
{
    Q_OBJECT
//friend class PrintData;
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    enum State{
        DISCONNECTED,
        CONNECTED,
        READY,
    };

    State state             = DISCONNECTED;
    QTimer *timeoutTimer    = nullptr;
    Bluetooth *bluetooth    = nullptr;
    Logger *logger          = nullptr;

    void startConnectTimeoutTimer();

public slots:
    void refreshDeviceList();
    void handleBluetoothConnect();
    void handleBluetoothDisconnect();
    void handleTransmitReady();
    void sendUserInput(char c);
    void handleAmpResults(const QVector<qreal> ampList);

private slots:
    void connectionTimeout();
    void on_RefreshDevicesButton_released();
    void on_ConnectButton_released();
    void readData(const QVector<qreal> dataList);
    void on_btnDataOutput_clicked();
    void on_btnImageClear_clicked();
    void on_btnClearRev_clicked();
    void receivedDataCount();
    void on_btnStop_clicked();
    void on_comboBoxAmpOfAxis_currentTextChanged(const QString &arg1);

signals:
    void ampOperator(const QVector<qreal>);
//    void printOperator(const Ui::MainWindow, const QList<qreal>);

public:
    //chart stuffs
    void InitChart();

private:
    //数据相关
    qreal step = 0.1;
    qreal datat = 0;
    int baseRange = 10;//默认x轴范围
    QVector<QTime>* vecTime=nullptr;
    QVector<qreal>* vecRelativeTime = nullptr;
    QVector<QVector<qreal>>* vecAccWithG = nullptr;
    QVector<QVector<qreal>>* vecAccNoG = nullptr;
    QVector<QVector<qreal>>* vecAng = nullptr;
    int RevCnt = 0;
    bool recFlag = true;

    //信息相关
    QTimer *m_timer;
    QLabel* labRevCnt;
    QLabel* labNote;

    //线程
    QThread axisAdjustThread;
    QThread dataDisposalThread;
};

#endif // MAINWINDOW_H
